package com.slkj.shilixiaoyuanapp.ui.teacher.tool.DisciplineInspection;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;

import com.google.gson.Gson;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.GradeEntity;
import com.slkj.shilixiaoyuanapp.entity.commitdata.NetScoreEntity;
import com.slkj.shilixiaoyuanapp.entity.contacts.ContactsPeopleEntity;
import com.slkj.shilixiaoyuanapp.net.HttpConfig;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.StudentAttendance.AttendanceListAdaper;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.StudentAttendance.StudentAttendanceHistoryActivity;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@ActivityInfo(layout = R.layout.activity_discipline_inspection, title = "纪律检查", rightHint = "历史记录")
public class DisciplineInspectionActivity extends BaseActivity {
    @BindView(R.id.recycer)
    RecyclerView recycer;
    GradeAdapter adapter;
    List<GradeEntity> data = new ArrayList<>();
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    @Override
    protected void init() {
        ((SimpleItemAnimator)recycer.getItemAnimator()).setSupportsChangeAnimations(false);
        adapter = new GradeAdapter(this,data);
        recycer.setLayoutManager(new LinearLayoutManager(this));
        recycer.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.HORIZONTAL));
        recycer.setAdapter(adapter);
        stateLayout.setOnReLoadListener(this::getData);
        getData();
    }

    private void getData(){
        HttpHeper.get().toolService()
                .getAllClass(2)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<List<GradeEntity>>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(List<GradeEntity> back) {
                        data.addAll(back);
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    protected void onRightTitleClick() {
        startActivity(DisciplineInspectionHistoryActivity.class);
    }
    @OnClick(R.id.tv_up)void up(){
        List<NetScoreEntity> scoreEntities = new LinkedList<>();
        for(GradeEntity gradeEntity : data){
            if (gradeEntity.getItemType() == GradeEntity.TYPE_GROUP){
                for (GradeEntity entity:gradeEntity.getYearClass()){
                    if (entity.getItemType() == GradeEntity.TYPE_CONTENT){
                        scoreEntities.add(new NetScoreEntity(entity.getId(),entity.getScore()));
                    }
                }
            }
        }
        if (scoreEntities.isEmpty()){
            showToast("未打分");
            return;
        }
        String json = new Gson().toJson(scoreEntities);
        HttpHeper.get().toolService()
                .addInspect(2,json)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String back) {
                        LoadSuccessAndFailDialog.showSuccess(DisciplineInspectionActivity.this,back);
                    }
                });
    }

}
